/* 
	--------------------------------------------------------------
	gm_order.js 2010-08-18 pt@gambio
	Gambio GmbH
	http://www.gambio.de
	Copyright (c) 2010 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
	--------------------------------------------------------------
*/
	
	function gm_mail_close(box) {
		$("#GM_" + box + "_BOX").hide('fast');	
	}

	function gm_mail_send(file, param, box) {
		
		var gm_subject	= $("#gm_subject").val();
		var gm_mail		= $("#gm_mail").val();
		$("#GM_" + box + "_BOX").html('<img src="../images/loading.gif" WIDTH="16" HEIGHT="16" BORDER="0" ALT="loading">');
		$("#GM_" + box + "_BOX").load(file + '?oID=' + oID + "&gm_mail=" + escape(gm_mail) + "&gm_subject=" + escape(gm_subject) + param + "&XTCsid=" + session_id);
	}

	function gm_cancel(file, param, box) {
		
		var gm_subject	= $("#gm_subject").val();
		var gm_mail		= $("#gm_mail").val();
		var gm_comments = $("#gm_comment").val();
		var gm_cancel_id = $("#gm_cancel_id").val();
		if( $("#gm_notify_comments").attr('checked') == true){
			var gm_notify_comments	= "on";
		}

		if( $("#gm_notify").attr('checked') == true){
			var gm_notify	= "on";
		}
		
		if( $("#gm_restock").attr('checked') == true){
			var gm_restock	= "on";
		}
		
		// BOF GM_MOD products_shippingtime:
		if( $("#gm_reshipp").attr('checked') == true){
			var gm_reshipp	= "on";
		}
		// BOF GM_MOD products_shippingtime:

		$("#GM_" + box + "_BOX").html('<img src="../images/loading.gif" WIDTH="16" HEIGHT="16" BORDER="0" ALT="loading">');
		$("#GM_" + box + "_BOX").load(file + '?oID=' + oID + "&gm_notify=" + escape(gm_notify) + "&gm_notify_comments=" + escape(gm_notify_comments) + "&gm_restock=" + escape(gm_restock) + "&gm_reshipp=" + escape(gm_reshipp) + "&gm_comments=" + escape(gm_comments) + "&gm_mail=" + escape(gm_mail) + "&gm_subject=" + escape(gm_subject) + param + "&XTCsid=" + session_id);
	}

	function gm_get_position(event, box) {

		position				= new Array(2);	
		var left				= 0;
		var top					= 0;
		var element_width		= $('#GM_' + box + '_BOX').outerWidth();
		var element_height		= $('#GM_' + box + '_BOX').outerHeight();

		var browser_width		= $(document).width();

		var browser_scroll_top	= $(document).scrollTop();
		var browser_scroll_left	= $(document).scrollLeft();

		
		var browser_height =  $(window).height();
			
			if(element_width + event.pageX > browser_width + browser_scroll_left) {
				position['left'] = browser_width + browser_scroll_left - element_width  - 30;	
			} else {
				position['left'] = event.pageX;
			}
			if(element_height + event.pageY > browser_height + browser_scroll_top) {
				position['top'] = browser_height + browser_scroll_top - element_height - 10;
			} else {
				position['top'] = event.pageY;
			}

		return position;
	}	

	$(document).ready(function() {

		$("#gm_check").click(function() {
			if($("#gm_check").attr("checked") == true) { 
				$("input.checkbox").attr("checked", "checked");	
			} else {
				$("input.checkbox").attr("checked", "");	
			}
		});

		$(".GM_INVOICE_MAIL").click(function(event) {
			var position = gm_get_position(event, 'INVOICE_MAIL');
			oID = $('#gm_order_id').val();

			$("#GM_INVOICE_MAIL_BOX").fadeIn('fast');
			$("#GM_INVOICE_MAIL_BOX").css({
				"position" : "absolute",
				"top":  position['top'] + "px",
				"left":  position['left']  + "px"
			});
			$("#GM_INVOICE_MAIL_BOX").load('gm_order_menu.php?' + 'oID=' + oID + '&XTCsid=' + session_id);			
		});
		
		$(".GM_SEND_ORDER").click(function(event) {
			oID = $('#gm_order_id').val();
			var position = gm_get_position(event, 'ORDERS_MAIL');
			$("#GM_ORDERS_MAIL_BOX").fadeIn('fast');

			$("#GM_ORDERS_MAIL_BOX").css({
				"position" : "absolute",
				"top":  position['top'] + "px",
				"left":  position['left']  + "px"
			});
			$("#GM_ORDERS_MAIL_BOX").load('gm_order_menu.php?' + 'oID=' + oID + '&type=order&XTCsid=' + session_id);			
		});

		$(".GM_CANCEL").click(function(event) {
			oID = $('#gm_order_id').val();
			var position = gm_get_position(event, 'CANCEL');
			$("#GM_CANCEL_BOX").fadeIn('fast');

			$("#GM_CANCEL_BOX").css({
				"position" : "absolute",
				"top":  position['top'] + "px",
				"left":  position['left']  + "px"
			});

			$("#GM_CANCEL_BOX").load('gm_order_menu.php?' + 'oID=' + oID + '&type=cancel&XTCsid=' + session_id);			
		});
	});